package com.lagou.edu.rest;

import com.lagou.edu.common.UserManager;
import com.lagou.edu.common.entity.vo.Result;
import com.lagou.edu.course.api.CourseRemoteService;
import com.lagou.edu.course.api.dto.CourseDTO;
import com.lagou.edu.course.api.dto.PageResultDTO;
import com.lagou.edu.course.api.param.CourseQueryParam;
import com.lagou.edu.entity.bo.UpLoadResult;
import com.lagou.edu.entity.form.CourseForm;
import com.lagou.edu.entity.vo.CourseVo;
import com.lagou.edu.service.impl.CourseService;
import com.lagou.edu.service.impl.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description 课程
 * @Author liuqiang
 * @Date 2023年01月23日 下午2:08:03
 */
@Api(tags = "课程", produces = "application/json")
@Slf4j
@Controller
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseRemoteService courseRemoteService;
    @Autowired
    private OssService ossService;
    @Autowired
    private CourseService courseService;

    @ApiOperation(value = "保存或者更新课程信息")
    @PostMapping("/saveOrUpdateCourse")
    @ResponseBody
    public Result<CourseDTO> saveOrUpdateCourse(@RequestBody CourseForm courseForm) {
        Integer userId = UserManager.getUserId();
        CourseDTO courseDTO = new CourseDTO();
        BeanUtils.copyProperties(courseForm, courseDTO);
        boolean flag = courseRemoteService.saveOrUpdateCourse(courseDTO, userId);
        return Result.success(flag);
    }

    @ApiOperation(value = "通过课程Id获取课程信息")
    @GetMapping("/getCourseById")
    @ResponseBody
    public Result<CourseVo> getCourseById(@RequestParam("courseId") Integer courseId) {
        Integer userId = UserManager.getUserId();
        CourseDTO courseDTO = courseRemoteService.getCourseById(courseId, userId);
        return Result.success(courseDTO);
    }

    @ApiOperation(value = "课程上下架")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "courseId", value = "课程ID"),
            @ApiImplicitParam(name = "status", value = "课程状态，0-草稿，1-上架")
    })
    @GetMapping("/changeState")
    @ResponseBody
    public Result changeState(@RequestParam("courseId") Integer courseId,
                              @RequestParam("status") Integer status) {
        return Result.success();
    }

    @ApiOperation(value = "分页查询课程信息")
    @PostMapping("/getQueryCourses")
    @ResponseBody
    public Result getQueryCourses(@RequestBody CourseQueryParam courseQueryParam) {
        PageResultDTO<CourseDTO> queryCourses = courseRemoteService.getQueryCourses(courseQueryParam);
        return Result.success(queryCourses);
    }

    @ApiOperation(value = "上传图片")
    @PostMapping("/upload")
    @ResponseBody
    public Result upload(@RequestParam("file") MultipartFile multipartFile) {
        UpLoadResult upLoadResult = ossService.upload(multipartFile);
        return Result.success(upLoadResult);
    }

}
